﻿using System;
using System.Collections.Generic;
using System.Threading;
using Steamworks;
using Terraria.Social.Base;

namespace Terraria.Social.Steam
{
	// Token: 0x02000130 RID: 304
	public class AchievementsSocialModule : AchievementsSocialModule
	{
		// Token: 0x06000D1A RID: 3354 RVA: 0x0000D2B5 File Offset: 0x0000B4B5
		public override void Initialize()
		{
			this._userStatsReceived = Callback<UserStatsReceived_t>.Create(new Callback<UserStatsReceived_t>.DispatchDelegate(this.OnUserStatsReceived));
			SteamUserStats.RequestCurrentStats();
			while (!this._areStatsReceived)
			{
				CoreSocialModule.Pulse();
				Thread.Sleep(10);
			}
		}

		// Token: 0x06000D1B RID: 3355 RVA: 0x0000D2EA File Offset: 0x0000B4EA
		public override void Shutdown()
		{
			this.StoreStats();
		}

		// Token: 0x06000D1C RID: 3356 RVA: 0x003F703C File Offset: 0x003F523C
		public override bool IsAchievementCompleted(string name)
		{
			bool flag;
			bool achievement = SteamUserStats.GetAchievement(name, ref flag);
			return achievement && flag;
		}

		// Token: 0x06000D1D RID: 3357 RVA: 0x003F7058 File Offset: 0x003F5258
		public override byte[] GetEncryptionKey()
		{
			byte[] array = new byte[16];
			byte[] bytes = BitConverter.GetBytes(SteamUser.GetSteamID().m_SteamID);
			Array.Copy(bytes, array, 8);
			Array.Copy(bytes, 0, array, 8, 8);
			return array;
		}

		// Token: 0x06000D1E RID: 3358 RVA: 0x0000D2F2 File Offset: 0x0000B4F2
		public override string GetSavePath()
		{
			return "/achievements-steam.dat";
		}

		// Token: 0x06000D1F RID: 3359 RVA: 0x003F7090 File Offset: 0x003F5290
		private int GetIntStat(string name)
		{
			int num;
			if (this._intStatCache.TryGetValue(name, out num))
			{
				return num;
			}
			if (SteamUserStats.GetStat(name, ref num))
			{
				this._intStatCache.Add(name, num);
			}
			return num;
		}

		// Token: 0x06000D20 RID: 3360 RVA: 0x003F70C8 File Offset: 0x003F52C8
		private float GetFloatStat(string name)
		{
			float num;
			if (this._floatStatCache.TryGetValue(name, out num))
			{
				return num;
			}
			if (SteamUserStats.GetStat(name, ref num))
			{
				this._floatStatCache.Add(name, num);
			}
			return num;
		}

		// Token: 0x06000D21 RID: 3361 RVA: 0x0000D2F9 File Offset: 0x0000B4F9
		private bool SetFloatStat(string name, float value)
		{
			this._floatStatCache[name] = value;
			return SteamUserStats.SetStat(name, value);
		}

		// Token: 0x06000D22 RID: 3362 RVA: 0x003F7100 File Offset: 0x003F5300
		public override void UpdateIntStat(string name, int value)
		{
			int intStat = this.GetIntStat(name);
			if (intStat < value)
			{
				this.SetIntStat(name, value);
			}
		}

		// Token: 0x06000D23 RID: 3363 RVA: 0x0000D30F File Offset: 0x0000B50F
		private bool SetIntStat(string name, int value)
		{
			this._intStatCache[name] = value;
			return SteamUserStats.SetStat(name, value);
		}

		// Token: 0x06000D24 RID: 3364 RVA: 0x003F7124 File Offset: 0x003F5324
		public override void UpdateFloatStat(string name, float value)
		{
			float floatStat = this.GetFloatStat(name);
			if (floatStat < value)
			{
				this.SetFloatStat(name, value);
			}
		}

		// Token: 0x06000D25 RID: 3365 RVA: 0x0000D325 File Offset: 0x0000B525
		public override void StoreStats()
		{
			SteamUserStats.StoreStats();
		}

		// Token: 0x06000D26 RID: 3366 RVA: 0x0000D32D File Offset: 0x0000B52D
		public override void CompleteAchievement(string name)
		{
			SteamUserStats.SetAchievement(name);
		}

		// Token: 0x06000D27 RID: 3367 RVA: 0x0000D336 File Offset: 0x0000B536
		private void OnUserStatsReceived(UserStatsReceived_t results)
		{
			if (results.m_nGameID == 105600UL && results.m_steamIDUser == SteamUser.GetSteamID())
			{
				this._areStatsReceived = true;
			}
		}

		// Token: 0x040011AB RID: 4523
		private const string FILE_NAME = "/achievements-steam.dat";

		// Token: 0x040011AC RID: 4524
		private Callback<UserStatsReceived_t> _userStatsReceived;

		// Token: 0x040011AD RID: 4525
		private bool _areStatsReceived;

		// Token: 0x040011AE RID: 4526
		private Dictionary<string, int> _intStatCache = new Dictionary<string, int>();

		// Token: 0x040011AF RID: 4527
		private Dictionary<string, float> _floatStatCache = new Dictionary<string, float>();
	}
}
